import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'



let instance = null

function render(props = {}) {
  const { container } = props
  
  instance = createApp(App)
  
  // 注册 Element Plus 图标
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    instance.component(key, component)
  }
  
  instance.use(createPinia())
  instance.use(router)
  instance.use(ElementPlus)
  
  instance.mount(container ? container.querySelector('#app') : '#app')
}

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  render()
}

export async function bootstrap() {
  console.log('[admin-app] bootstraped')
}

export async function mount(props) {
  console.log('[admin-app] mounted', props)
  render(props)
}

export async function unmount() {
  console.log('[admin-app] unmounted')
  instance?.unmount()
  instance = null
}

// 添加全局调试工具
if (process.env.NODE_ENV !== 'production') {
  window.debugApp = {
    router,
    getRouteInfo: () => {
      const route = router.currentRoute.value
      return {
        path: route.path,
        params: route.params,
        query: route.query,
        name: route.name
      }
    }
  }
  
  console.log('调试模式已启用，可以使用 window.debugApp 访问调试工具')
}

// 导入调试工具（仅在开发环境）
if (import.meta.env.DEV) {
  import('./utils/debug.js').catch(err => {
    console.error('加载调试工具失败:', err)
  })
}

// 确保模块有导出
export {};

